package com.example.hellodemo.widget;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.hellodemo.R;

import java.util.ArrayList;
import java.util.List;

public class CustomUIActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private Indicator indicator;
    private int[] imgs = {R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5};
    private List<View> views = new ArrayList();
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_ui);
        viewPager = findViewById(R.id.scrollPager);
        indicator = findViewById(R.id.scroolindicator);

        for (int i = 0;i < imgs.length; i++){
            View  view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_scrool,null);
            ImageView imageView = view.findViewById(R.id.img_view);
            imageView.setImageResource(imgs[i]);
            view.setTag(i);
            views.add(view);
        }
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return  Integer.MAX_VALUE;   // 实现无限循环滑动那个
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == object;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                View view = views.get(position % 5);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }
        });
//       viewPager.setOnScrollChangeListener(new ViewPager.OnScrollChangeListener() {
//
//           @Override
//           public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//
//                    indicator.changeIndex( views.indexOf(v));
//           }
//       });
       viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
           @Override
           public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

           }

           // 无限循环滑动
           @Override
           public void onPageSelected(int position) {

               indicator.changeIndex(position % 5);
           }

           @Override
           public void onPageScrollStateChanged(int state) {

           }
       });
    }
}
